package osmparser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;

/**
 * An OSM file contians a set of nodes, ways, and relations. Every node has an
 * id (string); every way has an id; every relation has an id. They are put into
 * three mappings from identifiers to objects for easier lookup.
 * 
 * @author Xiaohui Li
 * 
 * 
 */
public class OSMDoc {

	private Map<String, OSMNode> nodes;
	private Map<String, OSMWay> ways;
	private Map<String, OSMRelation> relations;

	public OSMDoc() {
		nodes = new HashMap<String, OSMNode>();
		ways = new HashMap<String, OSMWay>();
		relations = new HashMap<String, OSMRelation>();
	}

	public OSMDoc(Map<String, OSMNode> nodes, Map<String, OSMWay> ways,
			Map<String, OSMRelation> relations) {
		this.nodes = nodes;
		this.ways = ways;
		this.relations = relations;
	}

	public Map<String, OSMNode> getNodes() {
		return nodes;
	}

	public void setNodes(Map<String, OSMNode> nodes) {
		this.nodes = nodes;
	}

	public Map<String, OSMWay> getWays() {
		return ways;
	}

	public void setWays(Map<String, OSMWay> ways) {
		this.ways = ways;
	}

	public Map<String, OSMRelation> getRelations() {
		return relations;
	}

	public void setRelations(Map<String, OSMRelation> relations) {
		this.relations = relations;
	}

	public OSMNode getNode(String ref) {
		return nodes.get(ref);
	}

	public OSMWay getWay(String ref) {
		return ways.get(ref);
	}

	public OSMRelation getRelation(String ref) {
		return relations.get(ref);
	}
	
	
//	 /**
//     * @return The MultiLineString of all ways members of this relation. If any
//     *         way members can not be found in the datase, returns
//     *         <code>null</code>.
//     */
//    public Polygon getPolygon(String relationId) throws Exception{
////        List<Coordinate> lines = new ArrayList<Coordinate>();
//
//        for (AbstractNode member : relations.get(relationId).getMembers()) {
//            if (member.type.equals(Way.type)) {
//                Way way = (Way) member; 
//                List<Coordinate> coord = Arrays.asList(way.getLineString().getCoordinates());
//
//                if (!lines.isEmpty()) {
//                    Coordinate c = lines.get(lines.size() - 1);
//
//                    if (!c.equals(coord.get(0))) {
//
//                        if (c.equals(coord.get(coord.size() - 1))) {
//
//                            Collections.reverse(coord);
//
//                        } else {
//
//                            Collections.reverse(lines);
//                            c = lines.get(lines.size() - 1);
//                            
//                            if (!c.equals(coord.get(0))) {
//                                Collections.reverse(coord);
//                            }
//
//                        }
//
//                    }
//                }
//
//                lines.addAll(coord);
//            }
//        }
//
//        GeometryFactory fac = new GeometryFactory();
//        return fac.createPolygon(fac.createLinearRing(lines.toArray(
//                new Coordinate[0])), null);
//    }

	
}
